package com.example.tys.daggermvp.mvp.view;

import android.database.Cursor;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.example.tys.daggermvp.R;
import com.example.tys.daggermvp.bean.ImageBean;
import com.example.tys.daggermvp.utils.DisplayUtil;
import com.example.tys.daggermvp.utils.Singleton;
import com.example.tys.daggermvp.view.ImagePickerAdapter;
import com.example.tys.daggermvp.view.recyclerviewCompant.BaseAdapter;
import com.example.tys.daggermvp.view.recyclerviewCompant.ItemDivider;
import com.example.tys.daggermvp.view.recyclerviewCompant.sticky.StickyItemDivider;

import java.util.ArrayList;

/**
 * @author tys
 * @date 2018/4/29
 */

public class ImagePickerActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private RecyclerView rv;
    private ImagePickerAdapter adapter;
    private final static int LOAD_IMG = 0x1;
    private final static int LOAD_VIDEO = 0x10;
    private final static int LOAD_AUDIO = 0x100;
    private final static int LOAD_FILE = 0x1000;
    private final static String LOAD_PARAMS = "LOAD_PARAMS";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_picker_activity);
        initData(getIntent().getIntExtra(LOAD_PARAMS, LOAD_IMG));
        initView();
    }

    private void initView() {
        rv = findViewById(R.id.recyclerview);
        rv.setLayoutManager(new GridLayoutManager(this, 1));
        adapter = new ImagePickerAdapter(this, new ArrayList<ImageBean>(), 1);
        rv.addItemDecoration(new StickyItemDivider<>(adapter, 1));
        rv.setAdapter(adapter);
        adapter.setItemClickListen(new BaseAdapter.OnItemClickListen() {
            @Override
            public void onItemClick(View itemView, int position) {
                ImageBean item = adapter.getData(position);
                Toast.makeText(ImagePickerActivity.this, item.getFileName(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 通过ContentProvider加载本地文件
     *
     * @param load
     */
    private void initData(int load) {
        switch (load) {
            case LOAD_IMG:
                getSupportLoaderManager().initLoader(LOAD_IMG, null, this);
                break;
            case LOAD_VIDEO:
                getSupportLoaderManager().initLoader(LOAD_VIDEO, null, this);
                break;
            case LOAD_AUDIO:
                getSupportLoaderManager().initLoader(LOAD_AUDIO, null, this);
                break;
            default:
                getSupportLoaderManager().initLoader(LOAD_FILE, null, this);
                break;
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        CursorLoader cursorLoader;
        switch (id) {
            case LOAD_IMG:
                cursorLoader = new CursorLoader(this,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        ImageBean.PROJECTION, null, null,
                        MediaStore.Images.Media.DATE_MODIFIED + " DESC");
                break;
            case LOAD_VIDEO:
                cursorLoader = new CursorLoader(this,
                        MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        ImageBean.PROJECTION, MediaStore.Images.Media.DISPLAY_NAME, null,
                        MediaStore.Images.Media.DATE_MODIFIED + " DESC");
                break;
            case LOAD_AUDIO:
                cursorLoader = new CursorLoader(this,
                        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                        ImageBean.PROJECTION, null, null,
                        MediaStore.Images.Media.DATE_MODIFIED + " DESC");
                break;
            default:
                cursorLoader = new CursorLoader(this,
                        MediaStore.Files.getContentUri("external"), ImageBean.PROJECTION,
                        MediaStore.Images.Media.DATA + " like? and (" +
                                MediaStore.Images.Media.MIME_TYPE + " like? or " +
                                MediaStore.Images.Media.MIME_TYPE + " like?) and " +
                                MediaStore.Images.Media.SIZE + " >? and " +
                                MediaStore.Images.Media.DISPLAY_NAME + " !=? ",// 隐藏目录(.)中文件名为null
                        new String[]{"/storage/emulated/0/DCIM/%", "image/%", "video/%", "0", "null"},
                        MediaStore.Images.Media.DATE_MODIFIED + " DESC");
                break;
        }
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        Log.e("----", "count:" + data.getCount());
        while (data.moveToNext()) {
            //id
            String id = data.getString(data.getColumnIndex(ImageBean.PROJECTION[0]));
            //Path to the file on disk
            String filePath = data.getString(data.getColumnIndex(ImageBean.PROJECTION[1]));
            //name of the file
            String fileName = data.getString(data.getColumnIndex(ImageBean.PROJECTION[2]));
            //last modified
            long date = data.getLong(data.getColumnIndex(ImageBean.PROJECTION[3]));
            //The MIME type of the file
            String mimeType = data.getString(data.getColumnIndex(ImageBean.PROJECTION[4]));
            //The width of the image/video in pixels
            int width = data.getInt(data.getColumnIndex(ImageBean.PROJECTION[5]));
            //The height of the image/video in pixels
            int height = data.getInt(data.getColumnIndex(ImageBean.PROJECTION[6]));
            //The size of the file in bytes
            long size = data.getLong(data.getColumnIndex(ImageBean.PROJECTION[7]));
            ImageBean img = new ImageBean(id, filePath, fileName, date, mimeType, size, width, height);
            adapter.addDataAndNotify(img);
        }
        for (ImageBean imageBean : adapter.getData()) {
            Log.e("--111", DisplayUtil.formatDate("yyyy/MM/dd", imageBean.getModifiedDate() * 1000)
                    + ":" + imageBean.toString());
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        Log.e("----", "onLoaderReset");
    }

    public void bt(View view) {
        initData(getIntent().getIntExtra(LOAD_PARAMS, LOAD_FILE));
    }
}
